//
//  TDHistorySearchCellCell.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/5.
//  Copyright © 2018年 tonbright. All rights reserved.
//
#define innerHisH 32

#import "TDHistorySearchCellCell.h"
#import "TDHistoryInnerCell.h"
#import "TDHistorySearchInnerCell.h"
#import "NSString+Rect.h"
@interface TDHistorySearchCellCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *view_h;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end
@implementation TDHistorySearchCellCell

- (void)awakeFromNib {
    [super awakeFromNib];

    [self.tableView registerNib:[UINib nibWithNibName:@"TDHistorySearchInnerCell" bundle:nil] forCellReuseIdentifier:@"TDHistorySearchInnerCellId"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)setCollectionView {
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"TDHistoryInnerCell" bundle:nil] forCellWithReuseIdentifier:@"TDHistoryInnerCellId"];
    //    self.flowLayout.estimatedItemSize = CGSizeMake(60, 34);
    // 1.设置 最小行间距
    self.flowLayout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
}

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
//    [self.collectionView reloadData];
    [self.tableView reloadData];
    if (!_titleArray || _titleArray.count == 0) {
        _view_title.hidden = YES;
    }else {
        _view_title.hidden = NO;
    }
    
}


#pragma mark - 实现数据源方法
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TDHistorySearchInnerCell *historySearchInnerCell = [tableView dequeueReusableCellWithIdentifier:@"TDHistorySearchInnerCellId" forIndexPath:indexPath];
    historySearchInnerCell.lbe_title.text = self.titleArray[indexPath.row];
    return historySearchInnerCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titleArray[indexPath.item];
    if ([self.delegate respondsToSelector:@selector(historySearchCellSearchStrBtnClick:)]) {
        [self.delegate historySearchCellSearchStrBtnClick:title];
    }
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TDHistoryInnerCell *innerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TDHistoryInnerCellId" forIndexPath:indexPath];
    innerCell.lbe_historyText.text = self.titleArray[indexPath.item];
    return innerCell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titleArray[indexPath.item];
    if ([self.delegate respondsToSelector:@selector(historySearchCellSearchStrBtnClick:)]) {
        [self.delegate historySearchCellSearchStrBtnClick:title];
    }
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *title = self.titleArray[indexPath.item];
    CGFloat width = [NSString boundingRectWithText:title font:13 size:CGSizeMake(1000, 16)].width;
    return CGSizeMake(width + 40, 33.0f);
}

- (IBAction)cleanHistoryBtnClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(historySearchCellCleanBtnClick)]) {
        [self.delegate historySearchCellCleanBtnClick];
    }
}

@end
